package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"rbook/api/request"
	"rbook/globals"
	"rbook/intermal/model"
	"time"
)

func SearchShow(c *gin.Context) {
	param := request.Search{}
	if err := c.ShouldBind(&param); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 400,
			"mes":  "参数错误",
		})
		return
	}
	var data []model.SearchRecord
	globals.DB.Where("keyword like ?", "%"+param.Keyword+"%").Order("result_count desc").Find(&data)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"mes":  "数据展示成功",
		"data": data,
	})
}

func SearchTime(c *gin.Context) {
	time.AfterFunc(time.Second*5, func() {
		globals.DB.Where("created_at<=?", time.Now().AddDate(0, 0, -7)).Delete(&model.SearchRecord{})
		fmt.Println("定时任务开启", time.Now().Format("2006-01-01"))
	})
}
